package com.app.service.impl.user;

import com.app.domain.bo.user.UserBo;
import com.app.domain.page.Page;
import com.app.domain.po.user.UserPo;
import com.app.domain.vo.user.UserVo;
import com.app.framework.utils.CoBeanUtils;
import com.app.framework.utils.CollectionUtils;
import com.app.framework.utils.RedisUtils;
import com.app.framework.utils.TokenUtils;
import com.app.mapper.user.UserMapper;
import com.app.service.user.UserCommonService;
import com.app.service.user.UserService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import jakarta.annotation.Resource;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 用户方法实现
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, UserPo> implements UserService {

    @Resource
    private UserCommonService userCommonService;

    /**
     * 用户列表
     *
     * @param bo 入参
     */
    @Override
    @Cacheable(cacheNames = "user", key = "#bo.getPageNum")
    public Page<UserVo> lists(UserBo bo) {
        PageHelper.startPage(bo.getPageNum(), bo.getPageSize());
        List<UserVo> userVos = baseMapper.lists(bo);
        if (CollectionUtils.isEmpty(userVos)) {
            return Page.restPage(new ArrayList<>());
        }
        return Page.restPage(userVos);
    }

    /**
     * 用户搜索
     *
     * @param bo 入参
     */
    @Override
    public Page<UserVo> search(UserBo bo) {
        PageHelper.startPage(bo.getPageNum(), bo.getPageSize());
        List<UserVo> userVos = baseMapper.search(bo);
        return Page.restPage(userVos);
    }

    /**
     * 获取登录用户信息
     */
    @Override
    public UserVo getRedisUser() {
        return TokenUtils.getLoginUser();
    }

    /**
     * 根据ID获取用户信息
     *
     * @param userId 用户ID
     */
    @Override
    @Cacheable(cacheNames = "user_details", key = "#userId")
    public UserVo details(Long userId) {
        UserPo userPo = baseMapper.selectById(userId);
        return CoBeanUtils.copy(userPo, UserVo.class);
    }

    /**
     * 用户停封
     *
     * @param userId 用户ID
     */
    @Override
    @CacheEvict(cacheNames = "user", allEntries = true)
    public void stop(Long userId) {
        UserPo userPo = baseMapper.selectById(userId);
        Assert.notNull(userPo, "用户不存在");
        Boolean status = userPo.getStatus();
        userPo.setStatus(!status);
        baseMapper.updateById(userPo);
    }

    /**
     * 修改用户
     *
     * @param bo 入参
     */
    @Override
    public void updateOne(UserBo bo) {
        Long loginUserId = TokenUtils.getLoginUserId();
        Assert.state(bo.getId().equals(loginUserId), "不能修改Ta人资料");
        UserPo userPo = baseMapper.selectById(bo.getId());
        CoBeanUtils.copyProperties(bo, userPo);
        userPo.setUpdateTime(new Date());
        baseMapper.updateById(userPo);
    }

    /**
     * 用户点赞
     *
     * @param userId 用户ID
     */
    @Override
    @CacheEvict(cacheNames = "user_details", key = "#userId")
    public void like(Long userId) {
        Long loginUserId = TokenUtils.getLoginUserId();
        UserPo userPo = baseMapper.selectById(userId);
        Assert.notNull(userPo, "用户不存在");
        Assert.state(!userId.equals(loginUserId), "自己不能点赞自己");
        userCommonService.updateUserLikeCount(userId);
    }

    /**
     * 重载用户
     *
     * @param token token
     */
    @Override
    public void refresh(String token) {
        Long loginUserId = TokenUtils.getLoginUserId();
        UserPo userPo = baseMapper.selectById(loginUserId);
        Assert.notNull(userPo, "用户不存在");
        UserVo userVo = CoBeanUtils.copy(userPo, UserVo.class);
        //覆盖旧值(24小时过期)
        RedisUtils.setToken(token, userVo);
    }

}